package com.huabo.system.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.extension.api.R;
import com.hbfk.util.JsonBean;
import com.hbfk.util.ResponseFormat;
import com.huabo.system.oracle.entity.TblOrganizationInfo;
import com.huabo.system.oracle.entity.TblPersonalTrain;
import com.huabo.system.oracle.entity.TblStaff;
import com.huabo.system.service.TblJobService;
import com.huabo.system.service.TblOrganizaService;
import com.huabo.system.service.TblStaffService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.InputStreamReader;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

@RestController
@Slf4j
@Api(value = "审计人员管理接口Controller", tags = {"审计人员管理"})
public class ZcSynchronizationController {
    private static final Log logger = LogFactory.getLog(ZcSynchronizationController.class);
	@Resource
	private TblStaffService tblStaffService;
	
	@Resource
	private TblJobService  tblJobService;
	
	@Resource
	private TblOrganizaService TblOrganizaService;

	//人力资源同步
    @RequestMapping(value = "/saveOrUpdateStaff", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
    @ApiOperation(value = "审计人员管理-新增/修改")
    public JsonBean saveOrUpdateStaff(HttpServletRequest request,
    		@ApiParam(name="data",value="用户实体表",required=true)@RequestParam(value="data", required = true)String data,
    		@ApiParam(name="attIds",value="附件主键数组 示例1,2,3,4",required=false) @RequestParam(value="attIds", required = false) String attIds) {
        JsonBean jsonBean = null;
		try {
			jsonBean = this.tblStaffService.mergeStaffManageInfo(data,attIds);
		} catch (Exception e) {
			ResponseFormat.retParam(1,1000,e.getMessage());
		}
		return jsonBean;
    }
    
    @RequestMapping(value = "/getStaffPageList", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
    @ApiOperation("审计人员管理列表分页功能")
    public JsonBean getAuditPlanPageList(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="orgId",value="当前右上角切换登录的公司",required=true) @RequestParam("orgId")BigDecimal orgId,
    		@ApiParam(name="pageNumber",value="分页当前页数",required=false)@RequestParam(value = "pageNumber", required = false, defaultValue = "1") Integer pageNumber,
   		@ApiParam(name="pageSize",value="每页记录数",required=false)@RequestParam(value = "pageSize", required = false, defaultValue = "15") Integer pageSize,
   		@ApiParam(name="realName",value="查询条件",required=false)@RequestParam(value="realName", required = false)String realName) {
    	JsonBean jsonBean = null;
    	try {
			jsonBean = tblStaffService.getStaffList(orgId,token,pageNumber,pageSize,realName);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return jsonBean;
    }
    
    @RequestMapping(value = "/getStaffInfo", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
    @ApiOperation("审计人员管理--查看详情及修改页面回显数据")
    public JsonBean getStaffInfo(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="staffId",value="审计人员id",required=true)@RequestParam(value = "staffId", required = true) String staffId) {
    	JsonBean jsonBean = null;
    	try {
			jsonBean = tblStaffService.findSjStaffDetail(token,staffId);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return jsonBean;
    }
    

    @RequestMapping(value = "/mergePlanProjectManageInfo", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	@ApiOperation(value = "审计人员管理- 培训信息新增或修改")
	public JsonBean mergePlanProjectManageInfo(HttpServletRequest request,@ApiParam(name="data",value="人员培训信息实体",required=true)@RequestParam(value = "data", required = true)String data,
			 @ApiParam(name="staffId",value="审计人员主键",required=false) @RequestParam(value = "staffId", required = false) String staffId,
			 @ApiParam(name="trainattIds",value="附件主键数组",required=false) @RequestParam(value="trainattIds", required = false) String trainattIds,
			  @ApiParam(name="traindate",required=false)@RequestParam(value="traindate", required = false)String traindate){
		     JsonBean jsonBean = null;
			try {
				net.sf.json.JSONObject json=net.sf.json.JSONObject.fromObject(data);
				TblPersonalTrain train = (TblPersonalTrain) net.sf.json.JSONObject.toBean(json,TblPersonalTrain.class);
				train.setStaffid(staffId);
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				if(traindate != null && !"".equals(traindate)) {
					train.setTraintime(sdf.parse(traindate));
				}
				jsonBean = this.tblStaffService.saveOrUpdateTrain(train, staffId,trainattIds);
			} catch (Exception e) {
				ResponseFormat.retParam(1,1000,e.getMessage());
			}
			return jsonBean;
	} 
    
    @RequestMapping(value = "/removeTrain", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	@ApiOperation(value = "审计人员管理- 培训信息删除")
	public JsonBean removePlanProjectInfo(HttpServletRequest request,
			 @RequestParam(value = "trainId", required = true)@ApiParam(name="trainId",value="人员培训信息主键",required=true)String trainId,
			 @ApiParam(name="token",value="登录用户token",required=false) @RequestHeader("token")String token)throws Exception{
		 JsonBean jsonBean = null;
			try {
				jsonBean = tblStaffService.deleteTrain(token,trainId);
			} catch (Exception e) {
				ResponseFormat.retParam(1,1000,e.getMessage());
			}
			return jsonBean;
	}
    
    @RequestMapping(value = "/getStaffAttInfo", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
	@ApiOperation(value = "审计人员管理- 获取人员信息附件列表")
    public JsonBean getAuditPlanAttInfo(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="staffId",value="用户id ",required=true) @RequestParam(value = "staffId", required = false) String staffId) {
    	JsonBean jsonBean = null;
		try {
			jsonBean = this.tblStaffService.getStaffAttInfo(token,staffId);
		} catch (Exception e) {
			ResponseFormat.retParam(1,1000,e.getMessage());
		}
		return jsonBean;
    }
    
    /**
     * @description 根据attid删除附件
     * @author lyz
     * @date 2022/4/19 9:04
     */
    @RequestMapping(value = "/deleteStaffFileById", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
    @ApiOperation(value = "审计人员管理- 删除人员附件")
    public JsonBean deleteStaffFileById(HttpServletRequest request, HttpServletResponse response,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="attId",value="附件主键ID",required=true) @RequestParam("attId") String attId) throws Exception {
    	JsonBean jsonBean = null;
		try {
			jsonBean = this.tblStaffService.deleStaffAttInfo(token,attId);

		} catch (Exception e) {
			ResponseFormat.retParam(1,1000,e.getMessage());
		}
		return jsonBean;
    }
    
    
    /**
     * @description 根据attid删除附件
     * @author lyz
     * @date 2022/4/19 9:04
     */
    @RequestMapping(value = "/deleteFileById", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
    @ApiOperation(value = "审计人员管理- 删除培训资料附件")
    public JsonBean deleteFileById(HttpServletRequest request, HttpServletResponse response,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="attId",value="附件主键ID",required=true) @RequestParam("attId") String attId) throws Exception {
    	JsonBean jsonBean = null;
		try {
			jsonBean = this.tblStaffService.deleTrainAttInfo(token,attId);

		} catch (Exception e) {
			ResponseFormat.retParam(1,1000,e.getMessage());
		}
		return jsonBean;
    }
    
    
    @RequestMapping(value = "/organInfoDetail", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
    @ApiOperation("审计机构管理-单位基本情况查看详情及修改页面回显数据")
    public JsonBean organInfoDetail(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="orgId",value="左侧树orgid",required=true)@RequestParam(value = "orgId", required = true) String orgId) {
    	JsonBean jsonBean = null;
    	try {
			jsonBean = TblOrganizaService.findOrganInfoDetail(token,orgId);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return jsonBean;
    }
    
    
    
    @RequestMapping(value = "/getAuditorInformationList", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
    @ApiOperation("内部审计人员配备基本情况")
    public JsonBean getAuditorInformationList(HttpServletRequest request,
    		@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		@ApiParam(name="orgId",value="所属机构",required=true)@RequestParam(value = "orgId", required = true) String orgId) {
    	JsonBean jsonBean = null;
    	try {
			jsonBean = tblStaffService.getAuditorInformationList(token,orgId);
		} catch (Exception e) {
			e.printStackTrace();
		}
        return jsonBean;
    }
    
    
  //审计机构管理
    @RequestMapping(value = "/saveOrUpdateOrgan", produces = "application/json; charset=utf-8", method = {RequestMethod.POST})
    @ApiOperation(value = "审计机构管理-单位基本情况-新增/修改")
    public JsonBean saveOrUpdateOrgan(HttpServletRequest request,@ApiParam(name="token",value="登录用户token",required=true) @RequestHeader("token")String token,
    		TblOrganizationInfo info,@ApiParam(name="fill",required=false)@RequestParam(value="fill", required = false)String fill) {
        JsonBean jsonBean = null;
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			if(fill != null && !"".equals(fill)) {
				info.setFillingdate(sdf.parse(fill));
			}
		 	jsonBean = this.TblOrganizaService.updateOrganInfo(token,info);
		} catch (Exception e) {
			ResponseFormat.retParam(1,1000,e.getMessage());
		}
		return jsonBean;
    }
}
